package cn.gov.pbc.gd.credit.common.util;

import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.liu.tools.DateUtil;
import com.liu.tools.MD5Util;
import com.liu.tools.RandomUtil;
import com.liu.tools.StrUtil;

import cn.gov.pbc.gd.credit.common.constant.CommonConstant;
import cn.gov.pbc.gd.credit.common.constant.ConfConstant;
import cn.gov.pbc.gd.credit.common.model.UserModel;
import cn.gov.pbc.gd.credit.common.service.redis.IRedisService;
import cn.gov.pbc.gd.credit.vo.center.ResCCUserVo;
import cn.gov.pbc.gd.credit.vo.role.ResRoleVo;

public class UserUtil {

	private static Logger logger = LoggerFactory.getLogger(UserUtil.class);
	
	public static String getPwd(String userId, String pwd, String salt) {
		
		try {
			return MD5Util.MD5Salf(userId, pwd, salt);
		} catch (Exception e) {
			logger.error("UserUtil:getPwd","获取用户密码异常", e);
			return pwd;
		}
		
	}
	
	public static String getSid(String userId){
		
		String randomStr = RandomUtil.randomStr(9);
		
		return userId + randomStr;		
	}
	
	public static String getUserIdBySid(String sid){
		
		if(StrUtil.isNull(sid)){
			return "";
		}
		
		return sid.substring(0, sid.length() - 9);		
	}
	
	/**
	 * 真实用户登录缓存
	 * @param realAccount
	 * @param userId
	 * @param vo
	 * @param redisService
	 */
	public static void setRealUserLogin(String realAccount, String userId, ResCCUserVo vo, IRedisService redisService){
		
		redisService.setObject(realAccount + "_" + userId, vo, ConfConstant.realUserCacheTime);
	}
	
	/**
	 * 获取登录真实用户缓存
	 * @param realAccount
	 * @param userId
	 * @param redisService
	 * @return
	 */
	public static ResCCUserVo getRealUserLogin(String realAccount, String userId, IRedisService redisService){
		
		Object obj = redisService.getObject(realAccount + "_" + userId);
		
		if(obj == null){
			return null;
		}
		
		return JSON.parseObject(JSON.toJSONString(obj), ResCCUserVo.class);
	}
	
	public static ResCCUserVo getRealUserLogin(String sid, IRedisService redisService){
		
		UserModel userModel = getUserModel(sid, redisService);
	
		if(userModel == null){
			return null;
		}
		
		Object obj = redisService.getObject(userModel.getRealUserAccount() + "_" + userModel.getUserId());
		
		if(obj == null){
			return null;
		}
		
		return JSON.parseObject(JSON.toJSONString(obj), ResCCUserVo.class);
	}
	
	/**
	 * 获取登录真实用户缓存
	 * @param realAccount
	 * @param userId
	 * @param redisService
	 * @return
	 */
	public static boolean delRealUser(String realAccount, String userId, IRedisService redisService){
		
		redisService.delKey(realAccount + "_" + userId);
		
		return true;
	}
	
	/**
	 * 保存登录用户信息到缓存
	 * @param userModel
	 * @param redisService
	 */
	public static void setUserModel(UserModel userModel, IRedisService redisService){
		
		redisService.setObject(CommonConstant.CACHE_LOGIN_PREFIX + userModel.getUserId(), userModel, ConfConstant.sidCacheTime);
		//setOnLineUser(userModel, redisService);
	}
	
	/**
	 * 保存登录用户信息到缓存
	 * @param userModel
	 * @param redisService
	 */
	public static boolean delUserModel(String sid, IRedisService redisService){
		
		if(StrUtil.isNull(sid)){
			return true;
		}
		
		String userId = getUserIdBySid(sid);
		
		long flag = redisService.delKey(CommonConstant.CACHE_LOGIN_PREFIX + userId);
		if (flag > 0) {
			return true;
		} else {
			return false;
		}
		
	}
	
	/**
	 * @author liuxingmi
	 * @datetime 2017年7月2日 下午9:43:30
	 * @desc 设置在线用户 查看
	 * @param userModel
	 * @param redisService void
	 */
	@Deprecated
	public static void setOnLineUser(UserModel userModel, IRedisService redisService){
		
		UserModel onlineUser = new UserModel();
		onlineUser.setAccount(userModel.getAccount());
		onlineUser.setSid(userModel.getSid());
		onlineUser.setUserName(userModel.getUserName());
		onlineUser.setCorpId(userModel.getCorpId());
		onlineUser.setCorpName(userModel.getCorpName());
		onlineUser.setHeadUrl(userModel.getHeadUrl());
		onlineUser.setUserId(userModel.getUserId());
		
		redisService.setSortedSet(CommonConstant.CREDIT_USER_ONLINE, DateUtil.getCurrentFormatMill(), JSON.toJSONString(onlineUser));
	}
	
	/**
	 * @author liuxingmi
	 * @datetime 2017年7月2日 下午9:43:46
	 * @desc 获取指定时间内的在线用户数
	 * @param redisService
	 * @param startScore
	 * @param endScore
	 * @return long
	 */
	@Deprecated
	public static long countOnlineUser(IRedisService redisService, long startScore, long endScore){		
		return redisService.countSoredSet(CommonConstant.CREDIT_USER_ONLINE, startScore, endScore);
	}
	
	/**
	 * @author liuxingmi
	 * @datetime 2017年7月2日 下午9:44:04
	 * @desc 获取指定范围的用户列表
	 * @param redisService
	 * @param startRange
	 * @param endRange
	 * @return Set<String>
	 */
	@Deprecated
	public static Set<String> getOnlineUser(IRedisService redisService, long startRange,  long endRange){		
		return redisService.getSoredSet(CommonConstant.CREDIT_USER_ONLINE, startRange, endRange, true);
	}
	
	/**
	 * 
	 * @param sid
	 * @param redisService
	 * @return 获取登录用户缓存信息
	 */
	public static UserModel getUserModel(String sid, IRedisService redisService){
		
		if(StrUtil.isNull(sid)){
			return null;
		}
		
		String userId = getUserIdBySid(sid);
		Object object = redisService.getObject(CommonConstant.CACHE_LOGIN_PREFIX + userId);
		
		if(object == null){
			
			return null;
		}
		
		return JSON.parseObject(JSON.toJSONString(object), UserModel.class);
	} 
		
	/**
	 * @datetime 2017年8月29日 下午2:02:34
	 * @author liuxingmi
	 * @desc 判断角色类型
	 * @param list
	 * @return boolean
	 */
   public static boolean checkRoleType(List<ResRoleVo> list, Integer roleType){
		
		if(list == null || list.size() == 0){
			return false;
		}
		//角色类型：1-系统管理员，2-征信管理部门负责人，3-查询员, 4-分管征信行领导, 5-业务管理员
		for (ResRoleVo resRoleVo : list) {
			if(resRoleVo.getRoleType().equals(roleType)){
				return true;
			}			
		}
		
		return false;
	}
}
